package hello;




import java.util.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;



public class Mapa extends Canvas implements Runnable {

    private Sprite robot;
    private Mapa mapa;
    private int num=4;
    private String message;
    private Pantalla pantalla;
    private Display display;
	//private String delims = "[ ]+";
	private String[] instrucciones;
	int move;

    public Mapa(){
        //instanciamos (creamos) el objeto ball a partir de la clase Sprite
        robot= new Sprite(0,30,20,20);
    }
	public  void mapaScript(String movida){
		instrucciones = split(movida);
		for (int i = 0; i < instrucciones.length; i++){
			move = Integer.parseInt(instrucciones[i]);
			
			robot.mover(move, getHeight(), getWidth());
			
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
			repaint();
            serviceRepaints();
		}
	}
    private void initialize () {}
	
	private String[] split(String original) {
		Vector nodes = new Vector();
		String separator = " ";
		// Parse nodes into vector
		int index = original.indexOf(separator);
		while(index>=0) {
		nodes.addElement( original.substring(0, index) );
		original = original.substring(index+separator.length());
		index = original.indexOf(separator);
		}
		// Get the last node
		nodes.addElement( original );

			// Create splitted string array
		String[] result = new String[ nodes.size() ];
		if( nodes.size()>0 ) {
			for(int loop=0; loop<nodes.size(); loop++){
				result[loop] = (String)nodes.elementAt(loop);
			}
		}
	return result;
} 
	
	public boolean checkScript(String texto){		
		instrucciones = split(texto);
		String instr;
		boolean correcto = true;
		for (int i = 0; i < instrucciones.length; i++){
			move = Integer.parseInt(instrucciones[i]);			
			if(move!=1 && move!= 2 && move!=3 && move!=4){
				correcto = false;
				i = instrucciones.length;
			}		
		}
		return correcto;
	}


     public void keyPressed(int key)
    {
        switch ( getGameAction(key) ){
            case Canvas.UP:
             message = "up" ;
             num =1;
             robot.mover(num, getHeight(), getWidth());
            break;
           case Canvas.DOWN:
            message = "down";
            num = 2;
            robot.mover(num, getHeight(), getWidth());
            break;
          case Canvas.LEFT:
            message = "left";
            num =3;
            robot.mover(num, getHeight(), getWidth());
            break;
          case Canvas.RIGHT:
             message = "right";
             num = 4;
             robot.mover(num, getHeight(), getWidth());

             break;
        }

        repaint();
  }

 public void run(){
        while(true){
            //actualizamos la posicion de la bola

            //forzamos el repintado de la pantalla
            repaint();
            serviceRepaints();
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
            }
        }
    }


    public void paint(Graphics g){
		g.setColor(0x404040);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0, 0, 0);
		if(num==1)
			robot.draw1(g);
		if(num==2)
			robot.draw2(g);
		if(num==3)
			robot.draw3(g);
		if(num==4)
			robot.draw4(g);

    }
}
